﻿using KinonekoSoftware.CodariaDB.Documents.Relationships.Characters;

namespace KinonekoSoftware.Studio.Models.DataParts.Relationships
{
    public sealed class CharacterRelationshipPresenter : RelationshipSwitchable<CharacterRelationship>
    {
        private bool   _switchAppellation;
        private string _appellation;


        //-------------------------------------------------------------
        //
        //          ConstructAppellation Static Methods
        //
        //-------------------------------------------------------------
        #region ConstructAppellation

        private static string ConstructAppellation(string sourceName, string targetName, string appellation)
        {
            return LanguageManager.Language switch
            {
                LanguageArea.Chinese  => ConstructChineseAppellation(sourceName, targetName, appellation),
                LanguageArea.English  => ConstructEnglishAppellation(sourceName, targetName, appellation),
                LanguageArea.French   => ConstructFrenchAppellation(sourceName, targetName, appellation),
                LanguageArea.Germany  => ConstructGermanyAppellation(sourceName, targetName, appellation),
                LanguageArea.Korean   => ConstructKoreanAppellation(sourceName, targetName, appellation),
                LanguageArea.Japanese => ConstructJapaneseAppellation(sourceName, targetName, appellation),
                LanguageArea.Russian  => ConstructRussianAppellation(sourceName, targetName, appellation),
                _                     => ConstructChineseAppellation(sourceName, targetName, appellation),
            };
        }

        private static string ConstructChineseAppellation(string sourceName, string targetName, string appellation)
        {
            return $"{targetName}是{sourceName}的{appellation}";
        }

        private static string ConstructEnglishAppellation(string sourceName, string targetName, string appellation)
        {
            return $"{targetName}是{sourceName}的{appellation}";
        }

        private static string ConstructFrenchAppellation(string sourceName, string targetName, string appellation)
        {
            return $"{targetName}是{sourceName}的{appellation}";
        }

        private static string ConstructGermanyAppellation(string sourceName, string targetName, string appellation)
        {
            return $"{targetName}是{sourceName}的{appellation}";
        }

        private static string ConstructKoreanAppellation(string sourceName, string targetName, string appellation)
        {
            return $"{targetName}是{sourceName}的{appellation}";
        }

        private static string ConstructJapaneseAppellation(string sourceName, string targetName, string appellation)
        {
            return $"{targetName}是{sourceName}的{appellation}";
        }

        private static string ConstructRussianAppellation(string sourceName, string targetName, string appellation)
        {
            return $"{targetName}是{sourceName}的{appellation}";
        }

        #endregion


        //-------------------------------------------------------------
        //
        //          Private Methods
        //
        //-------------------------------------------------------------

        //-------------------------------------------------------------
        //
        //          Protected Methods
        //
        //-------------------------------------------------------------
        protected override void OnSwitchInstance()
        {
            //
            //
            (TargetCalledIt, SourceCalledIt) = (SourceCalledIt, TargetCalledIt);

            //
            //
            SwitchAppellation();
        }

        protected override void OnUpdate()
        {
            RaiseUpdated(nameof(Source));
            RaiseUpdated(nameof(SourceCalledIt));
            RaiseUpdated(nameof(Target));
            RaiseUpdated(nameof(TargetCalledIt));
            RaiseUpdated(nameof(IsBiDirection));
            RaiseUpdated(nameof(IsEditMode));
            RaiseUpdated(nameof(Appellation));
            RaiseUpdated(nameof(Type));
        }

        //-------------------------------------------------------------
        //
        //          Public Methods
        //
        //-------------------------------------------------------------
        
        public void ConstructAppellation()
        {

            if (_switchAppellation)
            {
                Appellation = ConstructAppellation(Target.Name, Source.Name, SourceCalledIt);
            }
            else
            {

                Appellation = ConstructAppellation(Source.Name, Target.Name, TargetCalledIt);
            }
        }
        
        /// <summary>
        /// 交换称谓。
        /// </summary>
        public void SwitchAppellation()
        {
            //
            // 变化
            _switchAppellation = !_switchAppellation;

            //
            //
            ConstructAppellation();
        }




        //-------------------------------------------------------------
        //
        //          Properties
        //
        //-------------------------------------------------------------

        public CharacterRelationshipType Type
        {
            get => Core.Type;
            set
            {
                Core.Type = value;
                RaiseUpdated();
            }
        }

        public string SourceCalledIt
        {
            get => InstanceSwitchFlag ? Core.TargetCalledIt : Core.SourceCalledIt;
            set
            {
                if (InstanceSwitchFlag)
                {
                    Core.TargetCalledIt = value;
                }
                else
                {
                    Core.SourceCalledIt = value;
                }
                RaiseUpdated();
            }
        }

        public bool IsBiDirection
        {
            get => Core.IsBiDirection;
            set
            {
                Core.IsBiDirection = value;
                RaiseUpdated();
            }
        }

        public string TargetCalledIt
        {
            get => InstanceSwitchFlag ? Core.SourceCalledIt : Core.TargetCalledIt;
            set
            {
                
                if (InstanceSwitchFlag)
                {
                    Core.SourceCalledIt = value;
                }
                else
                {
                    Core.TargetCalledIt = value;
                }
                
                RaiseUpdated();
            }
        }

        public string Appellation
        {
            get => _appellation; 
            private set => SetValue(ref _appellation, value);
        }
    }
}